package com.icode.lock.threadCommunication.test2;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: i_code
 * @Email: my_code@foxmail.com
 * @Date: 2020-12-10 17:40
 */

public class Data {
    private int num = 0;

    Lock lock = new ReentrantLock();
    Condition condition1 = lock.newCondition();
    Condition condition2 = lock.newCondition();
    Condition condition3 = lock.newCondition();

    public void printA() {
        lock.lock();
        try{
            while (num != 0){
                condition1.await();
            }
            System.out.println(Thread.currentThread().getName()+"-->AAAAAA");
            num ++;
            condition2.signal();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public void printB() {
        lock.lock();
        try {
            while (num != 1){
                condition2.await();
            }
            System.out.println(Thread.currentThread().getName()+"-->BBBBBB");
            num ++;
            condition3.signal();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
    public void printC() throws InterruptedException {
        lock.lock();
        try {
            while (num != 2){
                condition3.await();
            }
            System.out.println(Thread.currentThread().getName()+"-->CCCCCC");
            num = 0;
            condition1.signal();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}
